<?php
class DB {
	private $link;
	private $dbhost;
	private $dbuser;
	private $dbpsw;
	private $dbname;
	private $charset;
	private $goneaway = 5;
	
	public function DB($dbhost, $dbuser, $dbpsw, $dbname = '', $charset = 'utf8', $pconnect = 0, $new_link = true){
		$this->__construct($dbhost, $dbuser, $dbpsw, $dbname, $charset, $pconnect, $new_link);
	}
	
	/**
	 * 数据库操作类构造函数
	 *
	 * @param string $dbhost 主机地址或主机域名
	 * @param string $dbuser 用户名
	 * @param string $dbpsw 密码
	 * @param string $dbname 数据库
	 * @param tinyint $pconnect 连接方式
	 * @param bool $halt
	 * @return Object	DBase
	 */
	public function __construct($dbhost, $dbuser, $dbpsw, $dbname = '', $charset = 'utf8', $pconnect = 0, $new_link = true){		
		$this->dbhost	= $dbhost;
		$this->dbuser	= $dbuser;
		$this->dbpsw	= $dbpsw;
		$this->dbname	= $dbname;
		$this->charset	= $charset;
		
		$this->connect($pconnect, $new_link);
	}
	
	function connect($pconnect = 0, $new_link = true){
		if($pconnect) {
			if(!$this->link = mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpsw)) {
				$this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = mysql_connect($this->dbhost, $this->dbuser, $this->dbpsw, $new_link)) {
				$this->halt('Can not connect to MySQL server');
			}
		}
		$version = mysql_get_server_info($this->link);
		if($version > '4.1') {
			if($this->charset) {
				mysql_query("SET character_set_connection=".$this->charset.", character_set_results=".$this->charset.", character_set_client=binary", $this->link);
			}
			if($version > '5.0.1') {
				mysql_query("SET sql_mode=''", $this->link);
			}
		}
		if($this->dbname) {
			$this->select_db($this->dbname);
		}
	}
	
	/**
	 * 选择数据库
	 *
	 * @param string $dbname 数据库
	 * @return bool
	 */
	public function select_db($dbname = ''){
		$dbname = $dbname ? $dbname : $this->dbname;
		return mysql_select_db($dbname, $this->link);
	}
	
	/**
	 * 从结果集中取一个字段
	 *
	 * @param resource $result 结果集
	 * @return string
	 */
	public function result($result, $row = 0){
		return @mysql_result($result, $row);
	}
	
	/**
	 * 执行 SQL 查询
	 *
	 * @param string $sql SQL 语句
	 * @return mixed 资源标识符
	 */
	public function query($sql, $type = '') {
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(defined('DEBUG_PR') && DEBUG_PR == true){
			$GLOBALS['debug_info']['SQLs'][] = $sql;
		}
		if(defined('DEBUG_FB') && DEBUG_FB == true){
			fb($sql, 'SQLS');
		}
		
		if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
			$this->halt('MySQL Query Error', $sql);
		}
		return $query;
	}
	
	/**
	 * 计算记录集行数
	 *
	 * @param resource $result 结果集
	 * @return int
	 */
	public function num_rows($result){
		return mysql_num_rows($result);
	}
	
	/**
	 * 查询数据表列数，即字段数
	 *
	 * @param resource $result 结果集
	 * @return int
	 */
	public function num_fields($result){
		return mysql_num_fields($result);
	}
	
	/**
	 * 从结果集中取一行作为关联数组
	 *
	 * @param resource $result 结果集
	 * @return array MSSQL_ASSOC：关联数组，MSSQL_NUM：枚举数组，MSSQL_BOTH：两者
	 */
	public function fetch_array($result){
		return mysql_fetch_array($result, MYSQL_ASSOC);
	}
	
	/**
	 * 从结果集中取一行作为枚举数组
	 *
	 * @param resource $result 结果集
	 * @return array
	 */
	public function fetch_row($result){
		return mysql_fetch_row($result);
	}
	
	/**
	 * 释放结果集内存资源
	 *
	 * @param resource $result 结果集
	 * @return bool
	 */
	public function free_result($result){
		return mysql_free_result($result);
	}
	
	/**
	 * 移动内部结果的指针，使之可以获取指定的行数据
	 *
	 * @param resource $result 结果集
	 * @param int $row_number
	 * @return bool
	 */
	public function data_seek($result, $row_number){
		return mysql_data_seek($result, $row_number);
	}
	
	/**
	 * 初始化一个存贮过程
	 *
	 * @param resource $sp_name
	 * @return bool
	 */
	/*public function init($sp_name){
		return mysql_init($sp_name, $this->link);
	}*/
	
	/**
	 * 添加一个参数到存贮过程
	 *
	 * @param resource $stmt
	 * @param string $param_name
	 * @param mixed $mixed
	 * @param int $type
	 * @param int $is_output
	 * @param int $is_null
	 * @param int $maxlen
	 * @return bool
	 */
	/*public function bind($stmt, $param_name, $mixed, $type, $is_output = false, $is_null = false, $maxlen = 50){
		return mysql_bind($stmt, $param_name, $mixed, $type, $is_output, $is_null, $maxlen);
	}*/
	
	/**
	 * 执行一个存贮过程
	 *
	 * @param resource $stmt
	 * @return mixed
	 */
	/*public function execute($stmt){
		return mysql_execute($stmt);
	}*/
	
	/**
	 * 关闭数据库连接
	 *
	 * @return bool
	 */
	public function close() {
		return mysql_close($this->link);
	}
	
	/**
	 * 返回字符串
	 *
	 * @param string $msg
	 * @return string
	 */
	private function halt($message = '', $sql = ''){
		$error = mysql_error();
		$errorno = mysql_errno();
		if($errorno == 2006 && $this->goneaway-- > 0) {
			$this->connect($this->dbhost, $this->dbuser, $this->dbpsw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
			$this->query($sql);
		} else {
			$s = '';
			if($message) {
				$s = "<b>Info:</b> $message<br />";
			}
			if($sql) {
				$s .= '<b>SQL:</b>'.htmlspecialchars($sql).'<br />';
			}
			$s .= '<b>Error:</b>'.$error.'<br />';
			$s .= '<b>Errno:</b>'.$errorno.'<br />';
			exit($s);
		}
	}
	
	/**
	 * 取得上一步 INSERT 操作产生的 ID
	 *
	 * @return int
	 */
	public function insert_id() {
		return mysql_insert_id($this->link);
		
		/*$query = $this->query("SELECT @@IDENTITY AS last_insert_id");
		if($query){
			$row = $this->fetch_array($query);
			$this->free_result($query);
			Return $row['last_insert_id'];
		} else {
			return - 1;
		}*/
	}
	
	/**
	 * 返回前一次 MySQL 操作所影响的记录行数
	 * @return number
	 */
	public function affected_rows(){
		return mysql_affected_rows($this->link);
	}

	/**
	 * 加引号
	 * @param  mix $value 值
	 * @param  string $type
	 * @return mix
	 */
	public function quote($value, $type = ''){
		// 去除斜杠
		if(MAGIC_QUOTE){
			$value = stripslashes($value);
		}
		// 如果不是数字则加引号
		//if(!is_numeric($value)){
			$value = "'".mysql_real_escape_string($value)."'";
		//}
		return $value;
	}
	
}
?>
